# -*- coding: utf-8 -*-
# @Time      : 2024/5/9 9:12
# @Author    : 未命名
# @File      : testUrllib.py
# @Software  : PyCharm

'''
http中的一个请求库，不要额外安装
只需关注请求的参数、链接
提供了强大的解析功能
urllib.request 请求模块
urllib.error 异常处理模块
urllib.parse 解析模块
'''

#get请求
# import urllib.request

# response = urllib.request.urlopen("http://www.baidu.com")
# print(response)
# print(type(response))
# print(response.read())
# print("-"*40)
# #对获取的网页源码进行解码，否则会乱码
# print(response.read().decode("utf-8"))

# import urllib.parse
#
# dataExamp = bytes(urllib.parse.urlencode({"name":"petre","age":"18"}),encoding = "utf-8")
# response = urllib.request.urlopen("http://httpbin.org/post",data = dataExamp)
# print(response.read().decode('utf-8'))

# import urllib.error
# try:
#     response = urllib.request.urlopen("https://httpbin.org/get",timeout = 0.01)
#     print(response.read().decode('utf-8'))
# except urllib.error.URLError as e:
#     print("time out")

# import urllib.request
#
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.status)
# print(response.getheaders())
# for item in response.getheaders():
#     print(item)
# print(response.getheader("Server"))
# print(response.read().decode("utf-8"))

import urllib.request

urlExamp = "http://movie.douban.com/top250"
headersExamepl = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54"
}
rep = urllib.request.Request(url = urlExamp,headers = headersExamepl)
response = urllib.request.urlopen(rep)
print(response.read().decode("utf-8"))

